package com.ruoyi.piaopiao.service.impl;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.piaopiao.domain.ChartsDTO;
import com.ruoyi.piaopiao.domain.PartDTO;
import com.ruoyi.piaopiao.domain.Record;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;
import com.ruoyi.piaopiao.mapper.BillMapper;
import com.ruoyi.piaopiao.domain.Bill;
import com.ruoyi.piaopiao.service.IBillService;

/**
 * billService业务层处理
 *
 * @author 谢仕洲
 * @date 2025-06-21
 */
@Service
public class BillServiceImpl implements IBillService
{
    @Autowired
    private BillMapper billMapper;
    @Autowired
    private ResourceLoader resourceLoader;

    /**
     * 查询bill
     *
     * @param id bill主键
     * @return bill
     */
    @Override
    public Bill selectBillById(String id)
    {
        return billMapper.selectBillById(id);
    }

    /**
     * 查询bill列表
     *
     * @param bill bill
     * @return bill
     */
    @Override
    public List<Bill> selectBillList(Bill bill)
    {
        return billMapper.selectBillList(bill);
    }

    /**
     * 新增bill
     *
     * @param bill bill
     * @return 结果
     */
    @Override
    public int insertBill(Bill bill)
    {
        bill.setId(UUID.fastUUID().toString(true));
        bill.setCreateTime(DateUtils.getNowDate());
        bill.setCreateBy(SecurityUtils.getUsername());
        return billMapper.insertBill(bill);
    }

    /**
     * 修改bill
     *
     * @param bill bill
     * @return 结果
     */
    @Override
    public int updateBill(Bill bill)
    {
        bill.setUpdateTime(DateUtils.getNowDate());
        bill.setUpdateBy(SecurityUtils.getUsername());
        return billMapper.updateBill(bill);
    }

    /**
     * 批量删除bill
     *
     * @param ids 需要删除的bill主键
     * @return 结果
     */
    @Override
    public int deleteBillByIds(String[] ids)
    {
        return billMapper.deleteBillByIds(ids);
    }

    /**
     * 删除bill信息
     *
     * @param id bill主键
     * @return 结果
     */
    @Override
    public int deleteBillById(String id)
    {
        return billMapper.deleteBillById(id);
    }

    @Override
    public int addRecord(Record record) {
        record.setId(UUID.fastUUID().toString(true));
        return billMapper.addRecord(record);
    }

    @Override
    public List<Record> selectRecordList(Record record) {
        return billMapper.selectRecordList(record);
    }

    @Override
    public int updateRecord(Record record) {
        return billMapper.updateRecord(record);
    }

    @Override
    public void pay(Bill bill) {
        Bill b = billMapper.selectBillById(bill.getId());
        BigDecimal current = bill.getCurrent();
        if(current.compareTo(b.getRemain())<=0){
            BigDecimal result = b.getRemain().subtract(current);
            b.setRemain(result);
        }
        b.setState("1");
        b.setPayDate(null);
        b.setCurrent(BigDecimal.ZERO);
        billMapper.updateBill(b);
    }

    @Override
    public ChartsDTO getChartsData() {
        ChartsDTO chartsData = billMapper.getChartsData();
        List<PartDTO> partData = billMapper.getPartData();
        chartsData.setPartDTOS(partData);
        return chartsData;
    }
    @Override
    public void generateRecord() {
        List<Bill> bills = billMapper.selectBillList(null);
//        int month = LocalDate.now().getMonthValue();
        int month = 8;
        int year = LocalDate.now().getYear();
        int date = year * 100 + month;
        Record param = new Record();
        param.setMonth(date);
        List<Record> records = billMapper.selectRecordList(param);
        Map<String, Record> recordMap = records.stream().collect(Collectors.toMap(Record::getBillId, r -> r));
        for (Bill bill : bills) {
            if (bill.getState().equals("0")) {
                System.out.println("有账单未还款，不再生成还款计划");
                return;
            }
            Record record = recordMap.get(bill.getId());
            if(record!=null){
                bill.setState("0");
                bill.setCurrent(record.getAmount());
                bill.setPayDate(record.getPayDate());
                billMapper.updateBill(bill);
            }
        }
    }
}
